package com.shuqi.activity.viewport;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;

import com.shuqi.controller.R;

public class WebkitTost implements OnClickListener {
    private PopupWindow pop;
    private View view;
    private MyToastEvent listener;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    dissmiss();
                    break;
            }
        };
    };

    public WebkitTost(Context context) {
        view = View.inflate(context, R.layout.view_webkit_toast, null);
        pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        pop.setContentView(view);
    }

    public void show(View view) {
        pop.showAtLocation(view, Gravity.BOTTOM, 0, 120);
        handler.sendEmptyMessageDelayed(0, 3000);
    }

    public void dissmiss() {
        if (pop != null && pop.isShowing()) {
            pop.dismiss();
        }
    }

    /**
     * 当按钮点击的时候回调监听接口
     * 
     * @param listener
     */
    public void setOnclickListener(MyToastEvent listener) {
        this.listener = listener;
        view.findViewById(R.id.view_webkit_toast_jump).setOnClickListener(this);
        view.setClickable(true);
    }

    public interface MyToastEvent {
        public void onButtonClick();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_webkit_toast_jump:
                listener.onButtonClick();
                break;
        }

    }
}
